package com.mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dto.SmsSpikeProductDto;
import com.mall.entity.PmsProduct;
import com.mall.entity.SmsSpikeSession;
import com.mall.mapper.SmsSpikeCommodityMapper;
import com.mall.mapper.SmsSpikeSessionMapper;
import com.mall.service.PmsProductService;
import com.mall.service.SmsSpikeSessionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class SmsSpikeSessionServiceImpl extends ServiceImpl<SmsSpikeSessionMapper, SmsSpikeSession>  implements SmsSpikeSessionService {

    @Resource
    private SmsSpikeSessionMapper smsSpikeSessionMapper;

    @Resource
    private SmsSpikeCommodityMapper smsSpikeCommodityMapper;

    @Resource
    private PmsProductService pmsProductService;

    @Override
    public SmsSpikeSession getById(Integer id) {
        return smsSpikeSessionMapper.selectById(id);
    }

    @Override
    public List<SmsSpikeSession> getAll() {
        return smsSpikeSessionMapper.selectList(null);
    }

    @Override
    public boolean save(SmsSpikeSession smsSpikeSession) {
        if(smsSpikeSession.getId() == null)
        {
            return smsSpikeSessionMapper.insert(smsSpikeSession) > 0;
        }else{
            return  smsSpikeSessionMapper.updateById(smsSpikeSession) > 0;
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        return smsSpikeSessionMapper.deleteById(id) > 0;
    }

    @Override
    public boolean changeStatus(Integer id) {
        SmsSpikeSession smsSpikeSession = smsSpikeSessionMapper.selectById(id);
        Integer s = smsSpikeSession.getStatus();
        if(s==0)
        {
            smsSpikeSession.setStatus(1);
        }else{
            smsSpikeSession.setStatus(0);
        }
        return smsSpikeSessionMapper.updateById(smsSpikeSession) > 0;
    }

    @Override
    public List<SmsSpikeProductDto> findNowProducts() {
        LocalTime time = LocalTime.now();
        Integer nowSessionId =smsSpikeSessionMapper.getSessionId(time);
        List<Long> productIds = smsSpikeCommodityMapper.getProductId(nowSessionId);
        if(productIds.isEmpty())
        {
            System.out.println("产品为空");
            return null;
        }
        List<PmsProduct> list = pmsProductService.getListByIds(productIds);
        List<SmsSpikeProductDto> result = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            SmsSpikeProductDto productDto = new SmsSpikeProductDto();
            PmsProduct product = list.get(i);
            productDto.setProductSn(product.getProductSn());
            productDto.setDescription(product.getDescription());
            productDto.setDetailDesc(product.getDetailDesc());
            productDto.setDetailTitle(product.getDetailTitle());
            productDto.setId(product.getId());
            productDto.setPrice(product.getPrice());
            productDto.setName(product.getName());
            productDto.setPic(product.getPic());
            productDto.setPromotionPrice(smsSpikeCommodityMapper.getProductRelation(nowSessionId,product.getId()).getFlashPromotionPrice());
            productDto.setPromotionCount(smsSpikeCommodityMapper.getProductRelation(nowSessionId,product.getId()).getFlashPromotionCount());
            result.add(productDto);
        }
        return result;
    }

    @Override
    public String getEndTime() {
        LocalTime time = LocalTime.now();
        Integer nowSessionId =smsSpikeSessionMapper.getSessionId(time);
        SmsSpikeSession session  = smsSpikeSessionMapper.selectById(nowSessionId);
        if(session == null)
        {
            return null;
        }
        return session.getEndTime();
    }


}
